/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.elf.gwt.store.client.components;

import com.elf.gwt.store.client.StoreComponentController;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;


/**
 * Search Entry Box
 */
public class SearchComponent extends StoreSimpleComponent
{
	protected TextBox searchBox = null;
	protected Image searchImage = null;
	protected Label advancedSearchLabel = null;
	
	/**
	 * Constructor
	 */
	public SearchComponent(StoreComponentController controller, String stylePrefix)
	{
		super(controller, stylePrefix, null, false);

		Grid bodyPanel = new Grid(4, 1);
		bodyPanel.addStyleName("ComponentBodyPanel");
		bodyPanel.addStyleName(getHandle() + "BodyPanel");
		bodyPanel.getCellFormatter().addStyleName(0, 0, getHandle() + "TextBox");
		bodyPanel.getCellFormatter().addStyleName(1, 0, getHandle() + "Icon");
		bodyPanel.getCellFormatter().addStyleName(2, 0, getHandle() + "Text");
		bodyPanel.getCellFormatter().addStyleName(3, 0, getHandle() + "Advanced");
		bodyPanel.getCellFormatter().addStyleName(3, 0, "ComponentLink");
		bodyPanel.getCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_CENTER);
		bodyPanel.getCellFormatter().setHorizontalAlignment(1, 0, HasHorizontalAlignment.ALIGN_CENTER);
		bodyPanel.getCellFormatter().setHorizontalAlignment(2, 0, HasHorizontalAlignment.ALIGN_CENTER);
		bodyPanel.getCellFormatter().setHorizontalAlignment(3, 0, HasHorizontalAlignment.ALIGN_CENTER);

		searchBox = new TextBox();
		searchBox.addKeyUpHandler(searchKeyboardHandler);
		searchImage = new Image();
		searchImage.setTitle(controller.getStoreConstants().SearchComponentTitle());
		searchImage.addClickHandler(searchClickHandler);
		bodyPanel.setWidget(0, 0, searchBox);
		bodyPanel.setWidget(1, 0, searchImage);
		advancedSearchLabel = new InlineLabel();
		advancedSearchLabel.addClickHandler(advancedSearchClickHandler);
		bodyPanel.setWidget(3, 0, advancedSearchLabel);
		panel.setWidget(1, 0, bodyPanel);
		searchImage.setUrl(getImageDirectory() + "button_quick_find.gif");
		bodyPanel.setText(2, 0, controller.getStoreConstants().SearchComponentSearchText());
		advancedSearchLabel.setText(controller.getStoreConstants().SearchComponentSearchAdvanced());
	}
	
	public void build()
	{
		//searchBox.setText(controller.getAdvancedSearchModel().getKeywords());
		onBuilt();
	}

	protected KeyUpHandler searchKeyboardHandler = new KeyUpHandler() {
		public void onKeyUp(KeyUpEvent event) {
			if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
				String text = searchBox.getText();
				if (text.length() == 0)text = "*";
				searchBox.setText("");
				onSelection("AdvancedSearchResultsComponent--0--0--false--true--false--0--0--*--*--0--*--" + text);
			}
		}
	};

	protected ClickHandler searchClickHandler = new ClickHandler() {
		public void onClick(ClickEvent event) {
			String text = searchBox.getText();
			searchBox.setText("");
			if (text.length() == 0)text = "*";
			onSelection("AdvancedSearchResultsComponent--0--0--false--true--false--0--0--*--*--0--*--" + text);
		}
	};

	protected ClickHandler advancedSearchClickHandler = new ClickHandler() {
		public void onClick(ClickEvent event) {
			String text = searchBox.getText();
			searchBox.setText("");
			if (text.length() == 0)text = "*";
			onSelection("AdvancedSearchComponent--0--0--false--true--false--0--0--*--*--0--*--" + text);
		}
	};
}
